<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" />
    <style>
        .red {
            color: red !important;
        }

        .search {
            width: 300px;
            margin: 20px 0;
        }

        .my-form {
            display: flex;
            margin: 20px 0;
        }

        .my-form input {
            flex: 1;
            margin-right: 20px;
        }

        .table> :not(:first-child) {
            border-top: none;
        }

        .contain {
            display: flex;
            padding: 10px;
        }

        .list-box {
            flex: 1;
            padding: 0 30px;
        }

        .list-box a {
            text-decoration: none;
        }

        .echarts-box {
            width: 600px;
            height: 400px;
            padding: 30px;
            margin: 0 auto;
            border: 1px solid #ccc;
        }

        tfoot {
            font-weight: bold;
        }

        @media screen and (max-width: 1000px) {
            .contain {
                flex-wrap: wrap;
            }

            .list-box {
                width: 100%;
            }

            .echarts-box {
                margin-top: 30px;
            }
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="contain">
            <!-- 左侧列表 -->
            <div class="list-box">

                <!-- 添加资产 -->
                <form class="my-form">
                    <!-- 2.1添加功能双向绑定 -->
                    <input type="text" class="form-control" placeholder="消费名称" v-model="name" />
                    <input type="text" class="form-control" placeholder="消费价格" v-model="price" />
                    <!-- 2.2注册点击事件 -->
                    <button type="button" class="btn btn-primary" @click="add">添加账单</button>
                </form>

                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>编号</th>
                            <th>消费名称</th>
                            <th>消费价格</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- 1.2.使用拿到的数据渲染页面 -->
                        <tr v-for="(e,i) in list">
                            <td>{{i+1}}</td>
                            <td>{{e.name}}</td>
                            <!-- 1.3.设置动态类名 -->
                            <td :class="{red:e.price>200}">{{e.price}}.00</td>
                            <!-- 3.1删除设置点击 -->
                            <td><a href="javascript:;" @click.prevent="del(e.id)">删除</a></td>
                        </tr>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="4">消费总计： {{num}}.00</td>
                        </tr>
                    </tfoot>
                </table>
            </div>

            <!-- 右侧图表 -->
            <div class="echarts-box" id="main"></div>
        </div>
    </div>
    <script src="echarts.min.js"></script>
    <script src="vue.js"></script>
    <script src="axios.js"></script>
    <script>
        /**
             * 接口文档地址：
             * https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc-8df8-0aff8dc317a8/api-53371058
             * 
             * 功能需求：
             * 1. 基本渲染
             * 2. 添加功能
             * 3. 删除功能
             * 4. 饼图渲染
             */
        const app = new Vue({
            el: '#app',
            data: {
                // 新建数组储存响应到的数据
                list: [],
                name: '',
                price: '',
                myChart: null
            },
            methods: {
                // 取数据渲染页面 
                async myList() {
                    // 1.首先拿到数据
                    let { data: { data } } = await axios({
                        url: 'https://applet-base-api-t.itheima.net/bill',
                        params: {
                            creator: 'ccq'
                        }
                    })
                    this.list = data
                    this.myChart.setOption({
                        series: [
                            {
                                data: this.list.map(e => {
                                    return { value: e.price, name: e.name }
                                })
                            }
                        ]
                    })
                },
                // 3.2.删除功能实现
                async del(id) {
                    await axios({
                        url: `https://applet-base-api-t.itheima.net/bill/${id}`,
                        method: 'DELETE'
                    })
                    // 渲染页面
                    this.myList()
                },
                // 2.3.添加功能实现
                async add() {
                    // 2.4.非空判断
                    if (this.name === '' || this.price === '') {
                        alert('内容不能为空！')
                        return
                    }
                    await axios({
                        url: 'https://applet-base-api-t.itheima.net/bill',
                        method: 'POST',
                        data: {
                            creator: 'ccq',
                            name: this.name,
                            price: this.price
                        }
                    })
                    // 渲染页面
                    this.myList()
                    // 2.5清空表单
                    this.name = ''
                    this.price = ''
                }
            },
            // 计算总数
            computed: {
                num() {
                    return this.list.reduce((prev, e) => prev + e.price, 0)
                }
            },
            // 页面加载后请求
            async created() {
                // 渲染页面
                this.myList()
            },
            // 页面渲染后
            mounted() {
                this.myChart = echarts.init(document.querySelector('#main'));

                // 指定图表的配置项和数据
                let option = {
                    title: {
                        text: '花钱记账',
                        left: 'center'
                    },
                    tooltip: {
                    },
                    legend: {
                        bottom: 10,
                        left: 'center',
                    },
                    series: [
                        {
                            type: 'pie',
                            radius: '65%',
                            center: ['50%', '50%'],
                            selectedMode: 'single',
                            // 默认数据为空
                            data: [],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                this.myChart.setOption(option);
            }
        })
    </script>
</body>

</html>